package com.rengwuxian.materialedittext;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.MotionEvent;


/**
 * Created by Rich on 2017/7/17.
 */

public class PasswordEditText extends MaterialEditText {

    private Drawable eye;
    private Drawable openEye;
    private Drawable closeEye;
    private boolean see = false;


    public PasswordEditText(Context context) {
        super(context);
        initClearDrawable();
    }

    public PasswordEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PasswordEditText);
        if (typedArray != null) {
            openEye = typedArray.getDrawable(R.styleable.PasswordEditText_icon_open_eye);
            closeEye = typedArray.getDrawable(R.styleable.PasswordEditText_icon_close_eye);
            typedArray.recycle();
        }
        initIcon();
        initClearDrawable();
    }

    public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initIcon();
        initClearDrawable();
    }

    private void initIcon() {
        openEye.setBounds(0, 0,  (int) getResources().getDimension(R.dimen.m_eye_width), (int) getResources().getDimension(R.dimen.m_open_eye_height));//设置Drawable的宽高和TextSize的大小一致
        closeEye.setBounds(0, 0, (int) getResources().getDimension(R.dimen.m_eye_width), (int) getResources().getDimension(R.dimen.m_close_eye_height));//设置Drawable的宽高和TextSize的大小一致

    }

    private void initClearDrawable() {
        if (see) {
            eye = openEye;
            setInputType((InputType.TYPE_CLASS_TEXT));
        } else {
            eye = closeEye;
            setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
        setClearIconVisible();
    }

    private void setClearIconVisible() {
        //   Drawable right = visible ? mClearDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], eye, getCompoundDrawables()[3]);
        setCompoundDrawablePadding(30);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (eye != null && event.getAction() == MotionEvent.ACTION_UP) {
            int x = (int) event.getX();
            // 判断触摸点是否在水平范围内
            boolean isInnerWidth = (x > (getWidth() - getTotalPaddingRight() + 10));
            // 获取删除图标的边界，返回一个Rect对象
            Rect rect = eye.getBounds();
            // 获取删除图标的高度
            int height = rect.height();
            int y = (int) event.getY();
            // 计算图标底部到控件底部的距离
            int distance = (getHeight() - height) / 2;
            // 判断触摸点是否在竖直范围内(可能会有点误差)
            // 触摸点的纵坐标在distance到（distance+图标自身的高度）之内，则视为点中删除图标
            if (isInnerWidth) {
                see = !see;
                initClearDrawable();
            }
        }
        return super.onTouchEvent(event);
    }
}
